﻿Imports System.ServiceModel.DomainServices.Client

Partial Public Class NetejaAltaModificacio
    Inherits ChildWindow

    Private _Neteja As Web.Neteja
    Public ReadOnly Property Neteja As Web.Neteja
        Get
            Return _Neteja
        End Get
    End Property

    Private _Mode As EnumMode
    Public ReadOnly Property Mode As EnumMode
        Get
            Return _Mode
        End Get
    End Property

    Public Enum EnumMode
        ModeAlta
        ModeModificacio
        ModeTancament
    End Enum

#Region "Crear i Destruir"
    Public Sub New(pMode As EnumMode, pNeteja As Web.Neteja)
        MyBase.New()

        InitializeComponent()
        _Mode = pMode
        _Neteja = pNeteja
    End Sub


    Private Sub Child_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Closed
        Application.Current.RootVisual.SetValue(Control.IsEnabledProperty, True)
    End Sub

    Private Sub NetejaAlta_Loaded(sender As Object, e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        If RiaContext.Current.Authentication.User.Identity.IsAuthenticated = True Then
            Me.Activity.IsActive = True

            AddHandler Contexte.UsuarisCarregats, AddressOf UsuarisCarregats
            Contexte.LoadUsuaris()

            AddHandler Contexte.HostalsCarregats, AddressOf HostalsCarregats
            Contexte.LoadHostals()

            AmagarCamps()

            Me.gridDetall.DataContext = _Neteja
        End If
    End Sub

    Private Sub AmagarCamps()
        Select Case Mode
            Case EnumMode.ModeAlta
                Me.Title = StringLibrary.AltaNeteja

                lblHoraTancament.Visibility = Windows.Visibility.Collapsed
                lblUsuariFinalitzador.Visibility = Windows.Visibility.Collapsed
                txtHoraFi.Visibility = Windows.Visibility.Collapsed
                cboUsuariFinalitzador.Visibility = Windows.Visibility.Collapsed

            Case EnumMode.ModeModificacio
                Me.Title = StringLibrary.EditarNeteja

                If Not Neteja.EsTancat Then
                    lblHoraTancament.Visibility = Windows.Visibility.Collapsed
                    lblUsuariFinalitzador.Visibility = Windows.Visibility.Collapsed
                    txtHoraFi.Visibility = Windows.Visibility.Collapsed
                    cboUsuariFinalitzador.Visibility = Windows.Visibility.Collapsed
                End If

                If Not Contexte.UsuariActual.IdTipusUsuari = EnumTipusUsuari.Administrador Then
                    cboHostals.IsEnabled = False
                    cboUsuariCreador.IsEnabled = False
                    txtData.IsEnabled = False
                    cboUsuariFinalitzador.IsEnabled = False
                End If

            Case EnumMode.ModeTancament
                Me.Title = StringLibrary.TancarNeteja

                cboHostals.IsEnabled = False
                cboPersonalNeteja.IsEnabled = False
                cboUsuariCreador.IsEnabled = False
                txtHoraInici.IsEnabled = False
                txtData.IsEnabled = False

        End Select
    End Sub
#End Region

#Region "Carregar Dades"
    Private _HostalsCarregats As Boolean = False
    Private _UsuarisCarregats As Boolean = False

    Private Sub UsuarisCarregats(ByVal Resultat As Boolean, ByVal Missatge As String)
        RemoveHandler Contexte.UsuarisCarregats, AddressOf UsuarisCarregats

        Select Case Mode
            Case EnumMode.ModeAlta
                If Contexte.UsuariActual.IdTipusUsuari = EnumTipusUsuari.Administrador Then
                    cboUsuariCreador.ItemsSource = (From U As LullabyFullDeCaixa.Web.Usuari In Contexte.Usuaris Where U.IdTipusUsuari <> EnumTipusUsuari.Neteja).ToList
                Else
                    cboUsuariCreador.ItemsSource = {Contexte.UsuariActual}
                End If

                cboUsuariFinalitzador.ItemsSource = {_Neteja.UsuariFinalitzador}

            Case EnumMode.ModeModificacio
                If Contexte.UsuariActual.IdTipusUsuari = EnumTipusUsuari.Administrador Then
                    cboUsuariCreador.ItemsSource = (From U As LullabyFullDeCaixa.Web.Usuari In Contexte.Usuaris Where U.IdTipusUsuari <> EnumTipusUsuari.Neteja).ToList
                    cboUsuariFinalitzador.ItemsSource = (From U As LullabyFullDeCaixa.Web.Usuari In Contexte.Usuaris Where U.IdTipusUsuari <> EnumTipusUsuari.Neteja).ToList
                Else
                    cboUsuariCreador.ItemsSource = {_Neteja.UsuariCreador}
                    cboUsuariFinalitzador.ItemsSource = {_Neteja.UsuariFinalitzador}
                End If

            Case EnumMode.ModeTancament
                cboUsuariCreador.ItemsSource = {_Neteja.UsuariCreador}

                If Contexte.UsuariActual.IdTipusUsuari = EnumTipusUsuari.Administrador Then
                    cboUsuariFinalitzador.ItemsSource = (From U As LullabyFullDeCaixa.Web.Usuari In Contexte.Usuaris Where U.IdTipusUsuari <> EnumTipusUsuari.Neteja).ToList
                Else
                    cboUsuariFinalitzador.ItemsSource = {Contexte.UsuariActual}
                End If
        End Select


        _UsuarisCarregats = True
        If _HostalsCarregats And _UsuarisCarregats Then Me.Activity.IsActive = False
    End Sub

    Public Sub HostalsCarregats(ByVal Resultat As Boolean, ByVal Missatge As String)
        RemoveHandler Contexte.HostalsCarregats, AddressOf HostalsCarregats

        Select Mode
            Case EnumMode.ModeAlta
                If Contexte.UsuariActual.IdTipusUsuari = EnumTipusUsuari.Administrador Then
                    Me.cboHostals.ItemsSource = Contexte.Hostals
                Else
                    Me.cboHostals.ItemsSource = Contexte.HostalsPerUsuariActual.AsEnumerable
                End If

            Case EnumMode.ModeModificacio
                If Contexte.UsuariActual.IdTipusUsuari = EnumTipusUsuari.Administrador Then
                    Me.cboHostals.ItemsSource = Contexte.Hostals
                Else
                    Me.cboHostals.ItemsSource = {_Neteja.Hostal}
                End If

            Case Else
                Me.cboHostals.ItemsSource = {_Neteja.Hostal}

        End Select

        If Me.cboHostals.Items.Count > 0 AndAlso Mode = EnumMode.ModeAlta Then _Neteja.Hostal = DirectCast(Me.cboHostals.Items(0), Web.Hostal)

        _HostalsCarregats = True
        If _HostalsCarregats And _UsuarisCarregats Then Me.Activity.IsActive = False
    End Sub


#End Region

#Region "Guardar i Cancelar"
    Private Sub OKButton_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
        EliminarObjectErrors()

        If LullabyFullDeCaixa.Contexte.DomainContext.EntityContainer.HasChanges Then
            If ValidarDades() And Validacio.Errors.Count = 0 Then
                LullabyFullDeCaixa.Contexte.DomainContext.SubmitChanges(AddressOf DomainContext_SubmittedChanges, Nothing)
                Activity.IsActive = True
            End If
        Else
            Me.DialogResult = True
        End If
    End Sub

    Private Function ValidarDades() As Boolean
        Dim Resultat As Boolean = True

        If Neteja.UsuariNeteja Is Nothing Then
            Validacio.Errors.Add(New ValidationSummaryItem(StringLibrary.DebeSeleccionarUnUsuarioLimpieza))
            Resultat = False
        End If

        Return Resultat
    End Function

    Private Sub DomainContext_SubmittedChanges(ByVal s As SubmitOperation)
        Activity.IsActive = False

        If s.HasError Then
            App.ReportError(StringLibrary.ErrorGuardantDades)
        Else
            Me.DialogResult = True
        End If
    End Sub

    Private Sub CancelButton_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
        If LullabyFullDeCaixa.Contexte.DomainContext.EntityContainer.HasChanges Then
            LullabyFullDeCaixa.Contexte.DomainContext.RejectChanges()
        End If

        Me.DialogResult = False
    End Sub

    Public Sub EliminarObjectErrors()
        Dim i As Long

        i = 0
        While i < Validacio.Errors.Count
            If Validacio.Errors(i).ItemType = ValidationSummaryItemType.ObjectError Then
                Validacio.Errors.RemoveAt(i)
            Else
                i = i + 1
            End If
        End While
    End Sub
#End Region

#Region "Combo Usuaris Neteja"
    Private Sub cboHostals_SelectionChanged(sender As System.Object, e As System.Windows.Controls.SelectionChangedEventArgs)
        Dim DadesUsuariNeteja As List(Of Web.Usuari) = Nothing

        If e.AddedItems.Count > 0 Then
            If Mode = EnumMode.ModeAlta Then Neteja.UsuariNeteja = Nothing
            RefrescaDadesComboUsuarisNeteja()
        Else
            Me.cboPersonalNeteja.ItemsSource = Nothing
        End If
    End Sub

    Private Sub RefrescaDadesComboUsuarisNeteja()
        Dim DadesUsuariNeteja As List(Of Web.Usuari) = Nothing

        Select Case Mode
            Case EnumMode.ModeAlta
                Dim Hostal As Web.Hostal = cboHostals.SelectedItem
                DadesUsuariNeteja = (From UH As Web.UsuariHostal In Contexte.DomainContext.UsuariHostals Where UH.Usuari.IdTipusUsuari = EnumTipusUsuari.Neteja AndAlso UH.Hostal Is Hostal Select UH.Usuari Order By Usuari.Nom).ToList
                Me.cboPersonalNeteja.ItemsSource = DadesUsuariNeteja

            Case EnumMode.ModeModificacio
                Dim Hostal As Web.Hostal = cboHostals.SelectedItem
                DadesUsuariNeteja = (From UH As Web.UsuariHostal In Contexte.DomainContext.UsuariHostals Where UH.Usuari.IdTipusUsuari = EnumTipusUsuari.Neteja AndAlso UH.Hostal Is Hostal Select UH.Usuari Order By Usuari.Nom).ToList
                If Neteja.UsuariNeteja IsNot Nothing AndAlso Not DadesUsuariNeteja.Contains(Neteja.UsuariNeteja) Then DadesUsuariNeteja.Add(Neteja.UsuariNeteja)
                Me.cboPersonalNeteja.ItemsSource = DadesUsuariNeteja

            Case EnumMode.ModeTancament
                Me.cboPersonalNeteja.ItemsSource = {Neteja.UsuariNeteja}
        End Select
    End Sub
#End Region

End Class
